﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.Base.FlexField.ValueSet;
using UFIDA.U9.MRP.Enums;
using UFIDA.U9.SM.Ship;
using UFIDA.U9C.Cust.ZL.PlugBE.Tool;
using UFSoft.UBF.Business;
using UFSoft.UBF.Eventing;

namespace UFIDA.U9C.Cust.ZL.PlugBE
{
    /// <summary>
    /// 出货单审核 同步到OA系统
    /// </summary>
    class ShipCheckedZLExtend : IEventSubscriber
    {
        public void Notify(params object[] args)
        {
            if (args == null || args.Length == 0 || !(args[0] is UFSoft.UBF.Business.EntityEvent))
                return;
            BusinessEntity.EntityKey key = ((UFSoft.UBF.Business.EntityEvent)args[0]).EntityKey;
            if (key == null)
                return;
            UFIDA.U9.SM.Ship.Ship entity = key.GetEntity() as UFIDA.U9.SM.Ship.Ship;
            if (entity == null)
                return;
            try
            {
                if (PubWebClass.IsOpenPlug())
                {
                    if (entity.OriginalData.Status == ShipStateEnum.Approving && entity.Status == ShipStateEnum.Approved)
                    {
                        //同步到签收系统
                        MesShip model = new MesShip(entity);
                        string res = HttpUitls.Post(PubWebClass.mes_ShipUrl, JsonConvert.SerializeObject(model));
                    }
                }
            }
            catch (Exception ex)
            {
                PubWebClass.Log("同步出货单异常【" + entity.DocNo + "】：" + ex.Message);
            }

        }

        private class MesShip
        {
            public MesShip(UFIDA.U9.SM.Ship.Ship e)
            {
                this.groupCode = e.Org.Code;
                this.groupName = e.Org.Name;
                this.orderId = e.ID.ToString();
                this.orderType = e.DocumentType?.Name;
                this.outOrderNo = e.DocNo;
                this.outCount = "0";//总件数
                this.customerCode = e.OrderBy?.Code;
                this.customerName = e.OrderBy?.Name;
                this.customerAddr = e.OrderBy?.Customer?.OfficialLocation?.Name;
                this.outOrderDate = e.BusinessDate.ToString("yyyy-MM-dd");
                this.takeOver = e.OrderBy?.Customer?.DescFlexField.PrivateDescSeg4;
                
                this.takeOverArea = "";
                this.takeOverPhone = e.OrderBy?.Customer?.DescFlexField.PrivateDescSeg5;
                this.remark = e.ShipMemo;
                this.companyName = e.OrderBy?.Name;
                DefineValue defineValue = DefineValue.Finder.Find($" code='{e.DescFlexField.PubDescSeg21}' and ValueSetDef.Code='QHD' ");
                this.signTypeName = defineValue?.Name;
                this.signTypeCode = defineValue?.Code;
                this.@operator = UFSoft.UBF.Util.Context.PlatformContext.Current.UserCode;//操作人
                this.operatorDt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                this.takeOverType = "";//无需传值
                decimal sumCount = 0;
                string sono = "";
                this.takeOverAddress = e.DescFlexField.PubDescSeg16;//一次物流地址
                this.finalPhone = e.DescFlexField.PubDescSeg6;//二次物流电话
                this.finalAddress = e.DescFlexField.PubDescSeg4;//二次物流地址
                this.finale = e.DescFlexField.PubDescSeg5;//二次物流收货人
                this.customerOrderNo = e.DescFlexField.PubDescSeg2;//客户订单号
                this.takeOverUnit = e.DescFlexField.PubDescSeg3; //收货单位

                foreach (ShipLine i in e.ShipLines)
                {
                    sumCount += i.ShipQtyTUAmount;
                    if (!sono.Contains(i.SONo))
                    {
                        sono += (i.SONo + ",");
                    }
                    this.outDetails.Add(new MesShipLine
                    {
                        id = i.ID.ToString(),
                        materialNo = i.ItemInfo.ItemCode,
                        materialCode = i.ItemInfo.ItemID.ID.ToString(),
                        materialName = i.ItemInfo.ItemName,
                        materialModel = i.ItemInfo.ItemID.SPECS,
                        unit = i.TradeUom.Name,
                        saleCount = i.ShipQtyTUAmount.ToString(),
                        warehouseCode = i.WH?.Code,
                        warehouseName = i.WH?.Name,
                        remark = i.DescFlexField.PubDescSeg20


                    });
                }
                this.orderNo = sono;
                this.outCount = sumCount.ToString();
               
            }
            public string takeOverUnit { get; set; }
            public string groupCode { get; set; }
            public string groupName { get; set; }
            public string orderId { get; set; }
            public string orderType { get; set; }
            public string outOrderNo { get; set; }
            public string outCount { get; set; }
            public string customerCode { get; set; }
            public string customerName { get; set; }
            public string customerAddr { get; set; }
            public string outOrderDate { get; set; }
            public string takeOver { get; set; }
            public string takeOverAddress { get; set; }
            public string takeOverArea { get; set; }
            public string takeOverPhone { get; set; }
            public string takeOverType { get; set; }
            public string orderNo { get; set; }
            public string remark { get; set; }
            public string companyName { get; set; }
            public string signTypeName { get; set; }
            public string signTypeCode { get; set; }
            public string @operator { get; set; }
            public string operatorDt { get; set; }
            public string finalPhone { get; set; }
            public string finalAddress { get; set; }
            public string finale { get; set; }
            public string customerOrderNo { get; set; }
            public List<MesShipLine> outDetails = new List<MesShipLine>();

        }
        private class MesShipLine
        {
            public string id { get; set; }
            public string materialNo { get; set; }
            public string materialName { get; set; }
            public string materialCode { get; set; }
            public string materialModel { get; set; }
            public string unit { get; set; }
            public string saleCount { get; set; }
            public string warehouseCode { get; set; }
            public string warehouseName { get; set; }
            public string remark { get; set; }
        }
    }
}
